package com.example.wanbaqian.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

import com.example.wanbaqian.R;
import com.example.wanbaqian.activity.FindApplyActivity;
import com.example.wanbaqian.activity.FindApplyOneActivity;
import com.example.wanbaqian.activity.FindApplyTwoActivity;
import com.example.wanbaqian.activity.FindFreeActivity;
import com.example.wanbaqian.activity.FindInviteActivity;
import com.example.wanbaqian.activity.FindMessageActivity;
import com.example.wanbaqian.adapter.FragmentAdapter;
import com.example.wanbaqian.base.BaseFragment;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.bean.SharedBean;
import com.example.wanbaqian.bean.UserInfoBean;
import com.example.wanbaqian.customView.AutoAppBarLayout;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.SPUtils;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 发现fragment.
 */
public class FindFragment extends BaseFragment implements AppBarLayout.OnOffsetChangedListener {
    private static final String ARG_PARAM1 = "param1";
    @BindView(R.id.btn_a)
    AutoRelativeLayout btnA;
    @BindView(R.id.btn_b)
    AutoRelativeLayout btnB;
    @BindView(R.id.btn_c)
    AutoRelativeLayout btnC;
    @BindView(R.id.btn_d)
    AutoRelativeLayout btnD;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private static final String TAG = "FindFragment";
    @BindView(R.id.appbar)
    AutoAppBarLayout appbar;

    public static FindFragment newInstance(String param1) {
        FindFragment fragment = new FindFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_find;
    }

    List<Fragment> fragmentList = new ArrayList<>();
    String[] titles = {"我的会员", "代理排行"};
    FragmentAdapter adapter;

    @Override
    protected void initViewsAndEvents(View view) {
        setToolBar("发现");
        mToolbar.setNavigationIcon(null);
        fragmentList.clear();
        fragmentList.add(FindChildFragment.newInstance(titles[0]));
        fragmentList.add(FindChildFragment.newInstance(titles[1]));
        if (adapter == null) {
            adapter = new FragmentAdapter(getChildFragmentManager(), fragmentList, titles);
        }
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(1);
        tabLayout.setupWithViewPager(viewPager);
        appbar.addOnOffsetChangedListener(this);
    }

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    protected void DetoryViewAndThing() {

    }

    @OnClick({R.id.btn_a, R.id.btn_b, R.id.btn_c, R.id.btn_d})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_a:
                openActivity(FindMessageActivity.class);
                break;
            case R.id.btn_b:
                openActivity(FindFreeActivity.class);
                break;
            case R.id.btn_c:
                UserInfoBean infoBean = SPUtils.getUserInfo(mContext);
                Log.i(TAG, "onViewClicked: " + infoBean.getAgent_status());
                //[0未申请;1申请中;2审核通过;3审核不通过;4自动关闭]
                if (infoBean.getAgent_status() == 1) {
                    Bundle b = new Bundle();
                    b.putInt("TYPE", infoBean.getAgent_status());
                    openActivity(FindApplyTwoActivity.class, b);
                } else if (infoBean.getAgent_status() == 2)
                    openActivity(FindApplyActivity.class);
                else {
                    openActivity(FindApplyOneActivity.class);
                }
                break;
            case R.id.btn_d:
                HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.USER_QRCODE, mContext,
                        new String[]{"accessid"},
                        new String[]{SPUtils.getAccessid(mContext)}), mContext, null, CommonValue.REQUEST_CODE1, new HttpCallback() {
                    @Override
                    public void onSuccess(String content, Info info, int requestCode) {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("bean", JsonUtils.getInstance().jsonToObjectk(content, SharedBean.class));
                        openActivity(FindInviteActivity.class, bundle);
                    }

                    @Override
                    public void onCacheSuccess(String content, Info info, int requestCode) {

                    }

                    @Override
                    public void onError(Info info, int requestCode) {

                    }
                });
                break;
        }
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        Log.i(TAG, "onOffsetChanged: " + String.valueOf(verticalOffset >= 0));
        Log.i(TAG, "-------->off:" + verticalOffset + "  ScrollRange:" + appBarLayout.getTotalScrollRange() + "  height:" + appBarLayout.getHeight());
        Intent intent = new Intent();
        intent.setAction(CommonValue.FindFragmentReceiver);
        Bundle bundle = new Bundle();
        bundle.putBoolean("state", (-verticalOffset) >= appBarLayout.getTotalScrollRange());
        intent.putExtras(bundle);
        getActivity().sendBroadcast(intent);
    }
}
